import React, { forwardRef, useImperativeHandle, useRef } from "react";

export default function Expose() {
  const exposeRef = useRef(null);
  return (
    <div>
      <button onClick={() => exposeRef.current.changeValue("luyi")}>
        changeTextToLuyi
      </button>
      <button onClick={() => exposeRef.current.focus()}>focusInput</button>
      <FancyInput ref={exposeRef}></FancyInput>
    </div>
  );
}

const MyInput = (props, ref) => {
  const inputRef = useRef();
  const focus = () => {
    inputRef.current.focus();
  };
  const changeValue = (val) => {
    inputRef.current.value = val;
  };

  useImperativeHandle(ref, () => ({
    focus,
    changeValue,
  }));
  return (
    <div>
      <input ref={inputRef} />
    </div>
  );
};

const FancyInput = forwardRef(MyInput);
